package com.example;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import com.example.db.AddCategoryActivity;
import com.example.db.CategoryEntry;
import com.example.db.DbHelper;

/**
 * Created with IntelliJ IDEA.
 * User: wlai
 * Date: 12/12/12
 * Time: 8:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class CategoryList extends ListActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.category_list, menu);
        return true;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DbHelper dbHelper = new DbHelper(this);
//        dbHelper.addCategory("生活用品");
//        dbHelper.addCategory("水电费");
//        dbHelper.addCategory("吃饭");
//        dbHelper.addCategory("杂项");
        Cursor cursor = dbHelper.listCategory();
        this.setListAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1, cursor, new String[]{CategoryEntry.COLUMN_NAME_CATEGORY_NAME},
                new int[]{android.R.id.text1}));


       /* List l=new ArrayList();
        for(int i=0;i<10;i++){
            l.addCategory("aaaa"+i);
        }
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,android.R.id.text1,l));
   */

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "id=" + id, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if(item.getItemId()==R.id.addCategoryMenu){
            Intent intent=new Intent(this,AddCategoryActivity.class);
            startActivity(intent);
        }
        return true;
    }
}